﻿using PLCS.App.Shared.Extensions;
using PLCS.App.Shared.ViewModels;
using PLCS.Application.Contracts.ErrorLibraryApp.Dtos;
using PLCS.Client.IService.ConfigManager;
using System.Threading.Tasks;

namespace PLCS.App.ConfigManager.ViewModels.ErrorLibrary;

public class ErrorLibraryViewModel : NavigationPagedCurdViewModel<ErrorLibraryDto, ErrorLibraryEditDto, IErrorLibraryService>
{
    private string _kind;

    public string Kind
    {
        get { return _kind; }
        set { SetProperty(ref _kind, value); }
    }

    private string _code;

    public string Code
    {
        get { return _code; }
        set { SetProperty(ref _code, value); }
    }

    private string _source;

    public string Source
    {
        get { return _source; }
        set { SetProperty(ref _source, value); }
    }

    public ErrorLibraryViewModel(IErrorLibraryService service) : base(service)
    {
    }

    public override async Task GetPageData(int pageIndex)
    {
        try
        {
            UpdateLoading(true);
            var res = await _service.GetListBySearchAsync((pageIndex - 1) * PageSize, PageSize, Code, Kind, Source);
            TotalCount = (int)res.TotalCount;
            SetPageCount();
            DataGridList = res.Items.ToObservable();
        }
        finally
        {
            UpdateLoading(false);
        }
    }
}